package helper

import (
	"Hypnos-Soundcore/components/config"
	xlog "Hypnos-Soundcore/components/log"
	"Hypnos-Soundcore/constants"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"os"
	"path/filepath"
)

// const .
const (
	// 拆分文件周期
	CycleDuration = 3600
	// 拆分文件大小
	CycleFileSize = 500 * 1024 * 1024
)

// InitLogger init logger
func InitLogger(logConf config.LogConfig) {
	if logConf.ToFile {
		baseName := logConf.FileName
		dir, _ := filepath.Split(baseName)
		if len(dir) > 0 {
			fileErr := os.MkdirAll(dir, os.ModePerm)
			if fileErr != nil {
				fmt.Println(fileErr)
			}
		}
		logWriter, logErr := xlog.Open(baseName, CycleDuration, CycleFileSize, 0)
		if logErr == nil {
			gin.DefaultWriter = logWriter
			gin.DefaultErrorWriter = logWriter
		} else {
			fmt.Println(logErr)
		}
	}
	log.SetOutput(gin.DefaultWriter)
	xlog.SetOutput(gin.DefaultWriter)
	xlog.SetLevel(xlog.NameLevel(logConf.LevelName))
	xlog.SetFlags(0)
}

//func printStack() {
//	// at least 1 entry needed
//	pc := make([]uintptr, 10)
//	runtime.Callers(2, pc)
//	f := runtime.FuncForPC(pc[0])
//	file, line := f.FileLine(pc[0])
//	fmt.Printf("%s:%d %s\n", file, line, f.Name())
//}

// DebugPrintln print debug info
func DebugPrintln(tag string, a ...interface{}) {
	xlog.DebugPrintln(tag, a...)
}

// DebugPrintf print debug info
func DebugPrintf(tag string, format string, a ...interface{}) {
	xlog.DebugPrintf(tag, format, a...)
}

// DebugActionPrintf print error info
func DebugActionPrintf(tag string, actionID, format string, a ...interface{}) {
	xlog.Debugf(xlog.BuildLogV4(tag, actionID, format, a...))
}

// DebugActionPrintln print debug info
func DebugActionPrintln(tag string, actionID, actionSubID string, errMsg string, latency int64, a ...interface{}) {
	xlog.DebugActionPrintln(tag, actionID, actionSubID, errMsg, latency, a...)
}

// DebugAccessPrintln print debug info
func DebugAccessPrintln(bs constants.IBaseRequest, ex *constants.ExtraRequest) {
	xlog.DebugAccessPrintln(bs, ex)
}

// WarningPrintln print warning info
func WarningPrintln(tag string, a ...interface{}) {
	xlog.WarningPrintln(tag, a...)
}

// WarningPrintf print warning info
func WarningPrintf(tag string, format string, a ...interface{}) {
	xlog.WarningPrintf(tag, format, a...)
}

// WarningActionPrintln print debug info
func WarningActionPrintln(tag string, actionID, actionSubID string, errMsg string, latency int64, a ...interface{}) {
	xlog.WarningActionPrintln(tag, actionID, actionSubID, errMsg, latency, a...)
}

// WarningAccessPrintln print debug info
func WarningAccessPrintln(bs constants.IBaseRequest, ex *constants.ExtraRequest) {
	xlog.WarningAccessPrintln(bs, ex)
}

// ErrorPrintln print error info
func ErrorPrintln(tag string, a ...interface{}) {
	xlog.ErrorPrintln(tag, a...)
}

// ErrorPrintf print error info
func ErrorPrintf(tag string, format string, a ...interface{}) {
	xlog.ErrorPrintf(tag, format, a...)
}

// ErrorActionPrintf print error info
func ErrorActionPrintf(tag string, actionID, format string, a ...interface{}) {
	xlog.Errorf(xlog.BuildLogV4(tag, actionID, format, a...))
}

// ErrorActionPrintln print debug info
func ErrorActionPrintln(tag string, actionID, actionSubID string, errMsg string, latency int64, a ...interface{}) {
	xlog.ErrorActionPrintln(tag, actionID, actionSubID, errMsg, latency, a...)
}

// ErrorAccessPrintln print debug info
func ErrorAccessPrintln(bs constants.IBaseRequest, ex *constants.ExtraRequest) {
	xlog.ErrorAccessPrintln(bs, ex)
}

func Json(v interface{}) string {
	temp, _ := json.Marshal(v)
	return string(temp)
}
